<?php
namespace app\api\controller;
use app\common\controller\Base;
use app\common\exception\FailException;
use app\common\exception\ParameterException;
use app\common\exception\UserException;
use app\common\logic\User as UserLogic;
use app\common\logic\Collection as CollectionLogic;
use think\Request;
use think\Loader;

/**
 * @title 用户模块接口
 * @description 接口说明
 * @group 接口分组
 * @header name:token require:1 default: desc:token
 * @param name:user_type type:int require:1 default:1 other: desc:状态(1.会员登陆，2.商家，3.代理商，4.省级代理商，5.内部员工)
 */
class User extends Base
{

    /**
     * @title 获取用户信息
     * @description 接口说明
     * @author WCJ
     * @url /api/user/getUserInfo
     * @method POST
     *
     * @return id: 用户ID
     * @return username: 用户名
     * @return nickname: 用户昵称
     * @return mobile: 手机号
     * @return qq: QQ
     * @return salt: 密码盐
     * @return sex: 性别（1：男，2：女）
     * @return password: 密码（不体现）
     * @return email: 电子邮箱
     * @return birthday: 生日
     * @return reg_time: 注册时间
     * @return last_time: 最后一次登录时间
     * @return last_ip: 最后一次登录IP
     * @return pay_password: 支付密码（不提现）
     * @return token: token验证值
     * @return money:金额
     *
     *
     */
    public function getUserInfo(Request $request)
    {

        if($request->isPost()){

            $userid=$this->uid;
            if(!$userid){

                throw  new UserException(["code"=>201,"msg"=>"未登录状态",'errorCode' => 300]);

            }
            $user=new UserLogic($userid);
            $user_info=$user->get_users_byid($userid,$this->type);
            if($user_info["code"]!=200){
                throw  new UserException($user_info);
            }
            if($this->new_token){
                $user_info["new_token"]=$this->new_token;
            }
            return $user_info;
        }

    }

    /**
     * @title 修改用户信息
     * @description 接口说明
     * @author WCJ
     * @url /api/user/updateUserInfo
     * @method POST
     * @param name:nickname type:string require:1 default:0 other: desc:用户昵称
     * @param name:qq type:string require:1 default:0 other: desc:QQ
     * @param name:sex type:int require:1 default:0 other: desc:性别（1：男，2：女）
     * @param name:email type:string require:1 default:0 other: desc:电子邮箱
     * @param name:birthday type:string require:1 default:0 other: desc:生日
     *
     * @return id: 用户ID
     * @return username: 用户名
     * @return nickname: 用户昵称
     * @return mobile: 手机号
     * @return qq: QQ
     * @return salt: 密码盐
     * @return sex: 性别（1：男，2：女）
     * @return password: 密码（不体现）
     * @return email: 电子邮箱
     * @return birthday: 生日
     * @return reg_time: 注册时间
     * @return last_time: 最后一次登录时间
     * @return last_ip: 最后一次登录IP
     * @return money:金额
     *
     *
     */
    public function updateUserInfo(Request $request)
    {

        if($request->isPost()){

            $userid=$this->uid;
            $nickname=$request->param("nickname");
            $qq=$request->param("qq");
            $sex=$request->param("sex");
            $email=$request->param("email");
            $birthday=$request->param("birthday");
            $validate = Loader::validate("User");
            if (!$validate->scene("update")->check(['nickname' => $nickname,'qq'=>$qq,'sex'=>$sex,'email'=>$email,'birthday'=>$birthday])) {
                $ret=['code' => 201, 'msg' => $validate->getError(),'errorCode' => 404];
                throw new ParameterException($ret);
            }

            if(!$userid){

                throw  new UserException(['errorCode' => 300,"code"=>201,"msg"=>"未登录状态"]);

            }

            $user=new UserLogic($userid);
            $user_info=$user->update_userinfo($nickname,$qq,$sex,$email,$birthday);
            if($this->new_token){
                $user_info["new_token"]=$this->new_token;
            }
            return $user_info;
        }else{
            throw new ForbiddenException(['code' => 201,'errorCode' => 404,"msg"=>"请求错误"]);
        }

    }

    /**
     * @title 设置支付密码
     * @description 接口说明
     * @author WCJ
     * @url /api/user/updatePaypassword
     * @method POST
     * @param name:pay_password type:int require:0 default:1 other: desc:支付密码
     *
     * @return id: 用户ID
     *
     *
     */
     public function updatePaypassword(Request $request){
         $userid=$this->uid;
         $pay_password=$request->post("pay_password");


         $validate = Loader::validate("User");

         if (!$validate->scene("update_pay_password")->check(['user_id' => $userid,'pay_password'=>$pay_password])) {

             $ret=['code' => 201,'errorCode' => 404, 'msg' => $validate->getError()];
             throw new FailException($ret);

         }
         $pay_password=md5($pay_password);
         $user=new UserLogic($userid);
         $data=$user->update_paypassword($pay_password);
         if($this->new_token){
             $data["new_token"]=$this->new_token;
         }

         return $data;


     }


    /**
     * @title 修改密码
     * @description 接口说明
     * @author WCJ
     * @url /api/user/updatePassword
     * @method POST
     * @param name:ord_password type:int require:0 default:1 other: desc:旧密码
     * @param name:password type:int require:0 default:1 other: desc:密码
     *
     * @return id: 用户ID
     *
     *
     */
    public function updatePassword(Request $request){
        $userid=$this->uid;
        $ord_password=$request->post("ord_password");
        $password=$request->post("password");

        $validate = Loader::validate("User");

        if (!$validate->scene("update_password")->check(['user_id' => $userid,'password'=>$password,'old_password'=>$ord_password])) {

            $ret=['code' => 201,'errorCode' => 404, 'msg' => $validate->getError()];
            throw new FailException($ret);

        }
        $password=md5($password);
        $ord_password=md5($ord_password);
        $user=new UserLogic($userid);
        $data=$user->update_password($password,$ord_password);
        if($this->new_token){
            $data["new_token"]=$this->new_token;
        }

        return $data;


    }
  
    /**
     * @title 选择等级
     * @description 接口说明
     * @author WCJ
     * @url /api/user/changeRole
     * @method POST
     * @param name:rid type:int require:0 default:1 other: desc:等级ID，1、消费型，2、充值型，3、股东型
     *
     *
     *
     */
     public function changeRole(Request $request){
         $userid=$this->uid;
         $rid=$request->post("rid");

         $validate = Loader::validate("User");

         if (!$validate->scene("update_rid")->check(['user_id' => $userid, 'rid' => $rid])) {

             throw new ParameterException(['code' => 201,'errorCode' => 400, 'msg' => $validate->getError()]);
             die();
         }

         $user=new UserLogic($userid);
         $data= $user->change_role($userid,$rid);
         if($this->new_token){
             $data["new_token"]=$this->new_token;
         }
         return $data;
         

     }




    /**
     * @title 获取该用户是否收藏
     * @description 接口说明
     * @author WCJ
     * @url /api/user/is_user_collection
     * @method POST
     * @param name:shopid type:int require:0 default:1 other: desc:店铺id
     *
     * @return is_user: 是否收藏is_user
     *
     */
    public function is_user_collection(Request $request)
    {
        $userid = $this->uid;
        $shopid = $request->post("shopid");
        session("collection",null);

        $validate = Loader::validate("Common");
        if (!$validate->scene("common")->check(['id' => $userid])) {
            throw new ParameterException(['code' => 201,'errorCode' => 400, 'msg' => $validate->getError()]);
            die();
        }

        $validate = Loader::validate("Common");
        if (!$validate->scene("common")->check(['id' => $shopid])) {
            throw new ParameterException(['code' => 201,'errorCode' => 400,'msg' => $validate->getError()]);
            die();
        }

        $collectionlogic=new CollectionLogic();
        $is_user=$collectionlogic->is_user_collection($userid,$shopid);
        $data= ["code"=>200,"msg"=>"成功","is_user"=>$is_user];
        if($this->new_token){
            $data["new_token"]=$this->new_token;
        }
        return $data;
    }

    /**
     * @title 收藏或取消收藏
     * @description 接口说明
     * @author WCJ
     * @url /api/user/collect
     * @method POST
     * @param name:shopid type:int require:0 default:1 other: desc:店铺id
     *
     * @return is_user: 是否收藏is_user
     *
     */
    public function collect(Request $request)
    {
        $userid = $this->uid;
        $shopid = $request->post("shopid");

        if(session("collection")){
            throw new FailException(['code' => 201,'errorCode' => 404,"msg"=>"服务器超时"]);
        }

        session("collection",1);

        $validate = Loader::validate("Common");
        if (!$validate->scene("common")->check(['id' => $userid])) {
            throw new ParameterException(['code' => 201,'errorCode' => 400,'msg' => $validate->getError()]);
            die();
        }

        $validate = Loader::validate("Common");
        if (!$validate->scene("common")->check(['id' => $shopid])) {
            throw new ParameterException(['code' => 201,'errorCode' => 400,'msg' => $validate->getError()]);
            die();
        }

        $collectionlogic=new CollectionLogic();
        $collectdata=$collectionlogic->user_collect($userid,$shopid);
        session("collection",null);
        $datas= ['code' => 200, 'msg' => '成功',"data"=>$collectdata];
            if($this->new_token){
                $datas["new_token"]=$this->new_token;
            }
         return $datas;



    }
    /**
     * @title 我的团队
     * @description 接口说明
     * @author WCJ
     * @url /api/user/myTeam
     * @method POST
     * @param name:keyword type:int require:0 default:1 other: desc:搜索关键字
     *
     * @return data: 数据
     *
     */
    public function myTeam(Request $request){
        $userid = $this->uid;
        $keyword = $request->post("keyword");
        $validate = Loader::validate("Common");
        if (!$validate->scene("common")->check(['id' => $keyword])) {
            throw new ParameterException(['code' => 201,'errorCode' => 400,'msg' => $validate->getError()]);
            die();
        }
        $user=new UserLogic();
        $data= $user->my_team($userid,$keyword);
        if($this->new_token){
            $data["new_token"]=$this->new_token;
        }
        return $data;
    }

    /**
     * @title 我的代理团队
     * @description 接口说明
     * @author WCJ
     * @url /api/user/myAgentTeam
     * @method POST
     * @param name:keyword type:int require:0 default:1 other: desc:搜索关键字
     *
     * @return data: 数据
     *
     */
    public function myAgentTeam(Request $request){
        $userid = $this->uid;
        $keyword = $request->post("keyword");
        $validate = Loader::validate("Common");
        if (!$validate->scene("common")->check(['id' => $keyword])) {
            throw new ParameterException(['code' => 201,'errorCode' => 400,'msg' => $validate->getError()]);
            die();
        }
        $user=new UserLogic();
        $data= $user->my_agent_team($userid,$keyword);
        if($this->new_token){
            $data["new_token"]=$this->new_token;
        }
        return $data;
    }



    /**
     * @title 我的收藏列表
     * @description 接口说明
     * @author WCJ
     * @url /api/user/collection_list
     * @method POST
     * @param name:start type:int require:0 default:1 other: desc:页数
     * @param name:num type:int require:0 default:1 other: desc:每页个数
     *
     * @return data: 数据
     *
     */
    public function collection_list(Request $request){
        $userid = $this->uid;

        $start = $request->post('start') ? $request->post('start') : 1;
        $num = $request->post('num') ? $request->post('num') : 20;

        $collectionlogic=new CollectionLogic();
        $shop_array=$collectionlogic->getCollectionListByUserid($userid,$start,$num);
        $datas= ["code"=>200,"msg"=>"成功","data"=>$shop_array];
        if($this->new_token){
            $datas["new_token"]=$this->new_token;
        }
        return $datas;
    }


    /**
     * @title 验证支付密码
     * @description 接口说明
     * @author WCJ
     * @url /api/user/validate_password
     * @method POST
     * @param name:pay_password type:int require:1 default:1 other: desc:支付密码
     *
     * @return data: 数据
     *
     */
    public function validate_password(Request $request){
        $userid = $this->uid;
        $pay_password = $request->post('pay_password');
         if(!$pay_password){
            throw new ParameterException(['code' => 201,'errorCode' => 404,'msg' =>"支付密码不能为空"]);
            die();
         }
        $userlogic=new UserLogic();
        $pay_password=md5($pay_password);
        $data=$userlogic->yy_pay_password($userid,$pay_password);
        $datas= ["code"=>200,"msg"=>"成功","data"=>$data];
        if($this->new_token){
            $datas["new_token"]=$this->new_token;
        }
        return $datas;
    }


    /**
     * @title 绑定微信
     * @description 接口说明
     * @author WCJ
     * @url /api/user/bindwx
     * @method POST
     * @param name:openid type:int require:1 default:1 other: desc:openid
     * @param name:nickname type:int require:1 default:1 other: desc:微信昵称
     * @param name:head_src type:int require:1 default:1 other: desc:微信头像地址
     *
     * @return data: 数据
     *
     */
    public function bindwx(Request $request){
        $userid = $this->uid;
        $type=$this->type;
        $openid = $request->post('openid');
        $nickname =match_chinese($request->post('nickname'));
        $head_src = $request->post('head_src');
        if(!$openid){
            throw new ParameterException(['code' => 201,'errorCode' => 404,'msg' =>"绑定失败"]);
            die();
        }
        $userlogic=new UserLogic();;
        $data=$userlogic->bindwx($type,$userid,$openid,$nickname,$head_src);
        $datas= ["code"=>200,"msg"=>"成功","data"=>$data];
        if($this->new_token){
            $datas["new_token"]=$this->new_token;
        }
        return $datas;
    }



}